package com.example.presentshopping.ui.activity

import android.app.Dialog
import android.content.Context
import android.graphics.Color
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.text.TextUtils
import android.view.View
import android.view.inputmethod.InputMethodManager
import com.example.presentshopping.R
import com.example.presentshopping.config.base.BaseMvpActivity
import com.example.presentshopping.mvp.presenter.PublicPresenter
import com.example.presentshopping.net.http.HttpRequest
import com.example.presentshopping.net.http.HttpURL
import com.example.presentshopping.ui.custom.NoDoubleClickListener
import com.example.presentshopping.ui.dialog.MessageDialog
import com.example.presentshopping.utils.ToastUtil
import com.lljjcoder.citypickerview.widget.CityPicker
import kotlinx.android.synthetic.main.activity_payby_alipay.*
import kotlinx.android.synthetic.main.activity_shippingaddress.*

import kotlinx.android.synthetic.main.activity_turntable_address.*
import kotlinx.android.synthetic.main.activity_turntable_address.ex_address
import kotlinx.android.synthetic.main.activity_turntable_address.ex_name
import kotlinx.android.synthetic.main.activity_turntable_address.ex_phone
import kotlinx.android.synthetic.main.activity_turntable_address.ex_provinces
import kotlinx.android.synthetic.main.activity_turntable_address.title_main_tv

class TurntableAddressActivity : BaseMvpActivity() {

    var id :String ?=null
    var imm: InputMethodManager? = null
    private var queryInfoPresenter: PublicPresenter? = null

    override val layoutId: Int
        get() = R.layout.activity_turntable_address

    override fun initView(savedInstanceState: Bundle?) {
        queryInfoPresenter = PublicPresenter(context, false, "加载中")
        queryInfoPresenter?.attachView(this)

        title_main_tv?.leftBackImageTv?.setOnClickListener { finish() }
        title_main_tv?.titleTv?.text ="领取地址"
        id = intent.getStringExtra("id")
        ex_provinces.setOnClickListener {
            imm = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager?
            if (imm?.isActive!!) {
                imm?.hideSoftInputFromWindow(
                    it.getWindowToken(),
                    InputMethodManager.HIDE_NOT_ALWAYS
                );
                selectAddress();//调用CityPicker选取区域
            }
        }
        buttonPanel.setOnClickListener { serLocation() }
    }
    fun serLocation() {
        if (!TextUtils.isEmpty(ex_name.text)) {
            if (!TextUtils.isEmpty(ex_phone.text)) {
                if (!TextUtils.isEmpty(ex_provinces.text)) {

                    if (!TextUtils.isEmpty(ex_address.text)) {

                        queryInfoPresenter?.netWorkRequestPost(HttpURL.Req_addTurntableAddress,

                            HttpRequest.TurntableAddress(id,ex_name.text.toString(),ex_phone.text.toString(),ex_provinces.text.toString()+ex_address.text.toString()) )

                    } else {
                        showToast("请输入详细地址 ")
                    }

                } else {
                    showToast("请选择所在地区 ")
                }

            } else {
                showToast("请输入收件人联系电话 ")
            }

        } else {
            showToast("请输入收件人姓名 ")
        }
        //
    }

    override fun onDestroy() {
        queryInfoPresenter?.detachView()
        super.onDestroy()
    }

    private fun selectAddress() {
        val cityPicker: CityPicker = CityPicker.Builder(this@TurntableAddressActivity)
            .textSize(14)
            .title("地址选择")
            .titleBackgroundColor("#FFFFFF")
            .confirTextColor("#696969")
            .cancelTextColor("#696969")
            .textColor(Color.parseColor("#000000"))
            .provinceCyclic(true)
            .cityCyclic(false)
            .districtCyclic(false)
            .visibleItemsCount(7)
            .itemPadding(10)
            .onlyShowProvinceAndCity(false)
            .build()
        cityPicker.show()
        //监听方法，获取选择结果
        cityPicker.setOnCityItemClickListener { citySelected -> //省份
            val province = citySelected[0]
            //城市
            val city = citySelected[1]
            //区县（如果设定了两级联动，那么该项返回空）
            val district = citySelected[2]
            //邮编
            val code = citySelected[3]
            //为TextView赋值
            ex_provinces.setText(province.trim { it <= ' ' }  + city.trim { it <= ' ' }  + district.trim { it <= ' ' })
        }
    }

    override fun showToast(msg: String?) {
      ToastUtil.showMsg(msg)
    }

    override fun onFailure(e: Throwable?, code: Int, msg: String?, isNetWorkError: Boolean) {
        showToast(msg)
    }

    override fun onSuccess(data: String?) {
        MessDialog()
    }

    fun MessDialog(){
        MessageDialog.Builder(this)
             .setTvTitle("")
            .setTitle("")
            .setMessage("奖品领取地址添加成功，等待平台发货！")
            .setConfirm("确定")
            .setCancel("退出")
            .setListener(object :MessageDialog.OnListener{
                override fun onConfirm(dialog: Dialog?) {
                    finish()
                }

                override fun onCancel(dialog: Dialog?) {
                  finish()
                }
            }).show()


    }
}